<?
/**
* IntraDox.php
*
* @author  John Snook <jsnook@gmail.com>
* @package IntraDox2
* @subpackage includes
*/
//require_once($_SERVER['DOCUMENT_ROOT'] . "/QuickerData/db.php");
define("EDOC_PATH", "/ElectronicDocuments");

$db = new DBConnection("dbname=pdms port=5432 user=apache");
$Project = new CProject($db, $_SESSION["ProjectID"]);
if (!$Project->IsValid())
{
	//header("Location: index.php?Action=GetProject");
}
//print_r($Project);
/**
* CProject
*
* Stores our project information from page to page.
* @author  John Snook <jsnook@gmail.com>
* @version 2
* @copyright (c) Snook 2004
* @example /var/www/corp/IntraDox/Documents.php
* @package IntraDox
* @subpackage includes
*/
class CProject
{
	var $ID = NULL;
	var $Name = "";
	var $GuestList;
	/**
	* CProject Class Constructor
	* @param integer $DBConnection open database connection
	* @param $ID Primary key of the project to associate with
	*/
	function CProject($DBConnection, $ID)
	{
		if ($ID) 
		{
			//1st select the project
			$Query = $DBConnection->CreateQuery("Select project.project from project where project.id = $ID");
			if($row = $Query->FetchRow())
			{
				$this->ID = $ID;
				$this->Name = $row['project'];
			}
			$Query->Close();

			//then build the guestlist
			$Query = $DBConnection->CreateQuery("Select newfields_user.username from newfields_user inner join project_user on project_user.userid = newfields_user.id WHERE project_user.projectid = $ID");
			while($row = $Query->FetchRow())
			{
				$this->GuestList[] = $row['username'];
			}
//			print_r($this->GuestList);
			$Query->Close();
		}
		else
		{
			$this->ID = NULL;
		}
	}
	
	/**
	* IsValid - Cheap ass programming
	* @return bool True if we found a record in the contstructor
	*/
	function IsValid()
	{
		return $this->ID ? TRUE : FALSE;
	}
}

/** 
* Custom function to render a cell with an image related to what kind of file is in the system
*
*/
function EdocRender($Row, $Column, &$html)
{
	global $Project;
	$thefile = $Row['electronicdocument'];
	if (strlen($thefile))
	{
		$ext = strtolower(trim(substr($thefile, strrpos($thefile, '.') + 1)));
		$imagefile = "/Idox/filetype/16x16/" . $ext . ".gif";
		
		if ( ! file_exists($_SERVER['DOCUMENT_ROOT'] . $imagefile) )
		{
			$imagefile = "/Idox/filetype/16x16/unknown.gif";
		}
		//$thesize = size_hum_read(filesize($_SERVER['DOCUMENT_ROOT'] . "/ElectronicDocuments/IntraDox/{$Project->ID}/$thefile"));
		$html = "<div align=\"center\">";
		$html .= "<a href=\"" . EDOC_PATH . "/IntraDox/{$Project->ID}/" . htmlspecialchars(addslashes($thefile)) . "\">";
		$html .= "<img height=\"16\" width=\"16\" src=\"$imagefile\" hspace=\"0\" vspace=\"0\" border=\"0\" alt=\"$ext file, $thesize\" >";
		$html .= "</a>";
//		$html .= "<br /><span style=\"font-size:x-small;\">" . size_hum_read(filesize($_SERVER['DOCUMENT_ROOT'] . "/ElectronicDocuments/IntraDox/{$Project->ID}/$thefile</span>"));
		$html .= "</div>";
	}
	else
	{
		$html .= "&nbsp;";
	}
}

function size_hum_read($size){
/*
Returns a human readable size
*/
  $i=0;
  $iec = array("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
  while (($size/1024)>1) {
   $size=$size/1024;
   $i++;
  }
  $size = (int)$size;
  return substr($size,0,strpos($size,'.') + 4) . $iec[$i];
}

function QuickLinkRender($Row, $Column, &$html)
{
	$id = $Row['id'];
	$html = "<a href=\"QuickList.php?item={$id}\" target=\"QuickList\">+</a>\n";
}
?>
